package org.road0329.tools.proto_parse_tool.ems.xintian.utils;

import org.road0329.tools.proto_parse_tool.chargingpile.MsgDcsResult;
import org.road0329.tools.proto_parse_tool.ems.dataobject.gw.GwItemData;
import org.road0329.tools.proto_parse_tool.ems.dataobject.gw.ReqDataComm;
import org.road0329.tools.proto_parse_tool.utils.DateUtil;
import org.road0329.tools.proto_parse_tool.utils.ProtocolGW_09Util;
import org.road0329.tools.proto_parse_tool.utils.ProtocolUtils;
import org.road0329.tools.proto_parse_tool.utils.ProtocolXinTianUtil;
import org.road0329.tools.proto_parse_tool.vo.xtkj.MeterDataVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * Created by Administrator on 2018-09-13.
 */
public class XinTianClassOneUtil {

    private static Logger logger = LoggerFactory.getLogger(XinTianClassOneUtil.class);
    private static final int TARIFF_NUM_DEFAULT = 4;// 目前集中器按费率数=4 上送任务数据

    public static MsgDcsResult parseClassOneData(byte[] msg, int from, boolean acd)  {
        String commAddr = String.format("%02X%02X%02X%02X", msg[8], msg[7], msg[10], msg[9]);

        int index = from;
        int fixlen = (msg[index] & 0x80) == 0 ? 2 : 8;
        if (acd) {
            fixlen += 2;
        }
        index++;
        MsgDcsResult gwResult = new MsgDcsResult();
        List<GwItemData> gwItemList = new ArrayList<GwItemData>();
        while ((index + fixlen) < msg.length) {
            int fn = msg[index] & 0xff;
            index++;
            GwItemData item = new GwItemData();
            item.setFn(fn);
            try {
                ReqDataComm reqDataComm = new ReqDataComm();
                switch (fn) {
                    case 2:
                        reqDataComm.setValue("终端日历时钟:" + DateUtil.DateToLongStr(ProtocolGW_09Util.parseA1(msg, index)) + ";");// 终端抄表时间:yyyy-MM-dd HH:mm;
                        item.setData(reqDataComm);
                        index += 6;
                        break;
                    case 4:
                        Map<String,Object> parseResult = parseReport(msg, index);
                        item.setData(parseResult);
                        int len = (int)parseResult.get("len");
                        index += len;
                        break;
                    default:
                        logger.warn("376.1协议处理==> 一类类数据中找不到FN=" + fn + "的处理方法");
                }
                gwItemList.add(item);//新天平台数据上报， 移除havingTask检查
            } catch (Exception e) {
                logger.warn("376.1协议处理==> 一类类数据 解析错误.", e);
            }
        }
        gwResult.setData(gwItemList);
        gwResult.setSuccess(true);
        gwResult.setMessage("成功");
        return gwResult;
    }


    private static Map<String, Object> parseReport(byte[] byteValue, int index) {
        Map<String, Object> result = new LinkedHashMap<>();
        int oldIndex = index;
        int groupNum = ProtocolUtils.convertToUnsignedInt(byteValue[index + 1], byteValue[index]);
        result.put("groupNum", groupNum);
        index += 2;
        int groupIndex = ProtocolUtils.convertToUnsignedInt(byteValue[index + 1], byteValue[index]);
        result.put("groupIndex", groupIndex);
        index += 2;
        int meterNum = byteValue[index] & 0xff;
        index++;
        int meterType = byteValue[index] & 0xff;
        index++;
        logger.debug("F4 groupNum:{}, groupIndex:{}, meterNum:{}, meterType:{}", groupNum, groupIndex, meterNum, meterType);
        List<MeterDataVO> meterDataList = new ArrayList<>(meterNum);
        for (int i = 0; i < meterNum; i++) {
             meterDataList.add(ProtocolXinTianUtil.parseData(byteValue, index));
            index += 29;
        }
        result.put("meterDataList", meterDataList);
        int len = index - oldIndex;
        result.put("len", len);
        return result;
    }
}
